﻿using SHKF.B2C.Model.Entity.Center;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using static System.Net.Mime.MediaTypeNames;

namespace SHKF.B2C.BLL.Center
{
    public class ProductBaseImageBLL : BaseBll
    {
        public ProductBaseImageBLL(SqlSugarClient db) : base(db)
        {
            db.ChangeDatabase("B2C_Center_Image");
        }

        public List<ProductBaseImageEntity> Query(long FKSN)
        {
            List < ProductBaseImageEntity > list = this.db
                .Queryable<ProductBaseImageEntity>()
                .Where(ww=>ww.FKSN == FKSN).ToList();

            return list;
        }

        public ProductBaseImageEntity Add(ProductBaseImageEntity info)
        {
            info.pDate = DateTime.Now;
  
            //string root = AppDomain.CurrentDomain.BaseDirectory + "Images";
            //if(!Directory.Exists(root))
            //{
            //    Directory.CreateDirectory(root);
            //}

            ////2.创建图片文件夹
            //string Folder = info.Folder;
            //Folder = Folder.Replace("*", "-")
            //   .Replace("/", "-")
            //   .Replace("\\", "-")
            //   .Replace(":", "-")
            //   .Replace("?", "-")
            //   .Replace(">", "-")
            //   .Replace("<", "-")
            //   .Replace("!", "-")
            //   .Replace("\"", "-");

            //Folder = root + "/" + Folder;
            //if (!Directory.Exists(Folder))
            //{
            //    Directory.CreateDirectory(root);
            //} 

            //string fileName = info.ImageName;
            //fileName = fileName.Replace("*", "-")
            //    .Replace("/", "-")
            //    .Replace("\\", "-")
            //    .Replace(":", "-")
            //    .Replace("?", "-")
            //    .Replace(">", "-")
            //    .Replace("<", "-")
            //    .Replace("!", "-")
            //    .Replace("\"", "-");
            

            ////3.生成图片
            //if(info.Picture != null)
            //{
            //    string path2 = $"{Folder}/{fileName}.jpg";
            //    FileStream fs = new FileStream(path2, FileMode.Create);
            //    fs.Write(info.Picture, 0, info.Picture.Length);
            //    fs.Flush();
            //    fs.Close();

            //    info.Path = path2;
            //    info.Url = "";
            //}

            //保存数据库
            if (info.SN > 0)
            {
                //this.db.Updateable<ProductBaseImageEntity>(info).ExecuteCommand();
            }
            else
            {
                info.SN = this.db.Insertable<ProductBaseImageEntity>(info).ExecuteReturnBigIdentity();
            }


            return info;
        }

        public int Delete(ProductBaseImageEntity info)
        { 
            int result = this.db.Deleteable<ProductBaseImageEntity>().In(info.SN).ExecuteCommand();

            //删除硬盘图片
            //string path = info.Path;
            //if( File.Exists(path))
            //{
            //    System.IO.File.Delete(path);
            //} 
            return result;
        }
    }
}